package com.dykj.fireFighting.web.maintenanceplan;

import com.dykj.fireFighting.api.base.maintenanceplan.dto.MaintenancePlanDto;
import com.dykj.fireFighting.api.base.maintenanceplan.entity.MaintenancePlan;
import com.dykj.fireFighting.api.base.maintenanceplan.service.IMaintenancePlanService;
import com.dykj.fireFighting.api.base.maintenanceplan.vo.MaintenancePlanVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

import org.apache.dubbo.config.annotation.DubboReference;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;
/**
 * @Author: zly
 * @Description :
 * @Date: 2021-07-27
 */
@Slf4j
@Api(value = "接口", tags = {"接口"})
@RestController
@RequestMapping("maintenanceplan/maintenancePlan")
public class MaintenancePlanController {

    @DubboReference
    private IMaintenancePlanService maintenancePlanService;


    /**
     * 列表
     *
     * @return
     */
    @AutoLog(value = "-分页列表查询")
    @ApiOperation(value = "分页列表", notes = "分页列表")
    @GetMapping("/list")
    //@RequiresPermissions("maintenanceplan/maintenancePlan/list")
    public CommonResult<PageVo<MaintenancePlan>> treeGrid(MaintenancePlanDto maintenancePlanDto) {

        maintenancePlanDto.setMaintenanceDept(ShiroUtil.getUser().getEnterpriseId().toString());
        PageVo<MaintenancePlan> page = maintenancePlanService.queryList(maintenancePlanDto);
        return CommonResult.success(page);
    }


    /**
     * 列表
     *
     * @return
     */
    @AutoLog(value = "-分页预警列表查询")
    @ApiOperation(value = "分页预警列表", notes = "分页预警列表")
    @GetMapping("/warnList")
    //@RequiresPermissions("maintenanceplan/maintenancePlan/list")
    public CommonResult<PageVo<MaintenancePlanVo>> warnList(MaintenancePlanDto maintenancePlanDto) {
        LoginUserVo loginUserVo=ShiroUtil.getUser();
        if(loginUserVo.getUserType()!=0)
        {
            maintenancePlanDto.setMaintenanceDept(loginUserVo.getEnterpriseId().toString());
        }
        PageVo<MaintenancePlanVo> page = maintenancePlanService.queryWarnList(maintenancePlanDto);
        return CommonResult.success(page);
    }

    /**
     * 获取信息
     */
    @AutoLog(value = "-获取信息")
    @ApiOperation(value = "获取信息", notes = "获取信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    //@RequiresPermissions("maintenanceplan/maintenancePlan/info")
    public CommonResult<MaintenancePlan> info(@PathVariable("id") String id){
        MaintenancePlan maintenancePlan = maintenancePlanService.getById(id);
        return CommonResult.success(maintenancePlan);
    }

    /**
     * 保存信息
     */
    @AutoLog(value = "-保存信息")
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @PostMapping("/save")
    //@RequiresPermissions("maintenanceplan/maintenancePlan/save")
    public CommonResult save(@Valid @RequestBody MaintenancePlan maintenancePlan) {
        try {
            Document document = Jsoup.parse(maintenancePlan.getMaintenanceContent());
            String content = document.text();

            Document maintenanceName = Jsoup.parse(maintenancePlan.getMaintenanceName());
            String name = maintenanceName.text();
            maintenancePlan.setMaintenanceName(name);
            maintenancePlan.setCreateTime(new Date());
            maintenancePlan.setCreateBy(ShiroUtil.getUser().getId().toString());
            maintenancePlan.setMaintenanceUnitId(ShiroUtil.getUser().getEnterpriseId().toString());
            maintenancePlan.setMaintenanceContent(content);
            maintenancePlanService.save(maintenancePlan);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改信息
     */
    @AutoLog(value = "-修改信息")
    @ApiOperation(value = "修改信息", notes = "修改信息")
    @PostMapping("/update")
    //@RequiresPermissions("maintenanceplan/maintenancePlan/update")
    public CommonResult update(@Valid @RequestBody MaintenancePlan maintenancePlan) {
        try {
            maintenancePlan.setUpdateTime(new Date());
            maintenancePlan.setUpdateBy(ShiroUtil.getUser().getId().toString());
            Document document = Jsoup.parse(maintenancePlan.getMaintenanceContent());
            String content = document.text();
            maintenancePlan.setMaintenanceContent(content);
            maintenancePlanService.updateById(maintenancePlan);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除信息
     */
    @AutoLog(value = "-删除信息")
    @ApiOperation(value = "删除信息", notes = "删除信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    //@RequiresPermissions("maintenanceplan/maintenancePlan/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
            maintenancePlanService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
